﻿using System;
using System.Web.Mvc;
using System.Data.Linq;
using System.Collections;
using System.IO;
using System.Web.UI.WebControls;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;


namespace TSV.Library.Common
{
    public class ExportExcel
    {
        private string _fileName;
        private bool _is_render_no=true;

        private IEnumerable _list;
        private string[] _column_list= null;
        private string[] _header_excel = null;
        private string[] _colunm_text_type = null;
        private TableStyle _tableStyle;
        private TableItemStyle _headerStyle;
        private TableItemStyle _itemStyle;
        TableItemStyle _titleStyle ;
        private string _title = "";
        
        public bool IsRenderNo
        {
             set { _is_render_no = value; }
        }
        public string Title
        {
            set { _title = value; }
        }
        public string[] ColumnTextType
        {
            set { _colunm_text_type = value; }
        }
        public string[] ColumnList
        {
            set { _column_list = value; }
        }
        public string[] HeaderExcel
        {
            set { _header_excel = value; }
        }

        public string FileName
        {
            set { _fileName=value; }
        }

        public IEnumerable List
        {
            set { _list=value; }
        }

        

        public ExportExcel(TableItemStyle titleStyle, TableStyle tableStyle, TableItemStyle headerStyle, TableItemStyle itemStyle)
        {
           
            _tableStyle = tableStyle;
            _headerStyle = headerStyle;
            _itemStyle = itemStyle;
            _titleStyle = titleStyle;
            if (_titleStyle == null)
            {
                _titleStyle = new TableItemStyle();
                _titleStyle.ForeColor = Color.DarkBlue;
                _titleStyle.Font.Bold = true;
                _titleStyle.Font.Name = "Arial";
                _titleStyle.Font.Size = 18;

               
            }
            // provide defaults
            if (_tableStyle == null)
            {
                _tableStyle = new TableStyle();
                _tableStyle.Font.Name = "Arial";
                _tableStyle.BorderWidth = 0;
                _tableStyle.CellPadding = 0;
                _tableStyle.CellSpacing = 0;
            }
            if (_headerStyle == null)
            {
                
                _headerStyle = new TableItemStyle();
                _headerStyle.BorderStyle = BorderStyle.Groove;
                _headerStyle.BorderColor = Color.Black;
                _headerStyle.BorderWidth = Unit.Parse(".5pt");
                _headerStyle.BackColor = Color.DarkGray;
                _headerStyle.Font.Bold = true;
                _headerStyle.Font.Size = 10;
            }
            if (_itemStyle == null)
            {
                _itemStyle = new TableItemStyle();
                _itemStyle.BorderColor = Color.Black;
                _itemStyle.BorderStyle = BorderStyle.Groove;
                _itemStyle.BorderWidth = Unit.Parse(".5pt");
                _itemStyle.Font.Size = 9;
                             
            }
        }
        public ExportExcel():this (null, null, null, null)
        {
            
        }        

        private static void WriteFile(string fileName, string contentType, string content)
        {
            try
            {
                HttpContext context = HttpContext.Current;
                context.Response.Clear();
                context.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
                string style = @"<style> .text { mso-number-format:\@;} </style> ";
                context.Response.Write(style);
                context.Response.Write("<meta http-equiv=Content-Type content='text/html; charset=utf-8' />");
                context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                context.Response.ContentType = contentType;
                context.Response.Write(content);
                context.Response.End();
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
        }
    }
    
}